<template>
  <div>
    <h1>login</h1>
    <div id="loginWarp">
      <el-form :model="loginForm" :label-position="labelPosition" :rules="rules" ref="loginForm" label-width="100px"
        class="loginForm">
        <el-form-item label="账号" prop="username">
          <el-input type="text" v-model="loginForm.username" autocomplete="off" placeholder="请输入账号">账号</el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input type="password" v-model="loginForm.pass" autocomplete="off" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
          <el-button @click="gotoRegister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import ajax from "@/components/ajax";
import md5 from "js-md5";

export default {
  name: "login",
  data() {
    var checkName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "",
        pass: ""
      },
      labelPosition: "right",
      rules: {
        username: [
          {
            validator: checkName,
            trigger: "blur"
          }
        ],
        pass: [
          {
            validator: validatePass,
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      var that = this;
      this.$refs[formName].validate(valid => {
        if (valid) {
          const data = this.$refs[formName].model;

          let formData = new FormData();
          formData.append("username", data.username);
          formData.append("pass", md5(data.pass));

          ajax({
            method: "post",
            url: "http://127.0.0.1:3000/login",
            data: formData,
            withCredentials: true,
            success: function(res) {
              if (res.status === 0) {
                that.$message({
                  message: "登陆成功",
                  type: "success"
                });
                window.sessionStorage.setItem("username", res.data.username);
                // 登陆成功, 返回主页
                that.gotoHome();
              } else {
                that.$message.error({
                  message: "账号或密码错误"
                });
              }
            }
          });
        } else {
          console.log("error submit!");
          return false;
        }
      });
    },
    gotoRegister() {
      this.$router.push({
        path: "/register"
      });
    },
    gotoHome() {
      this.$router.push({
        path: "/"
      });
      window.sessionStorage.setItem("isLogin", "true");
    }
  }
};
</script>
<style>
#loginWarp {
  display: flex;
  justify-content: center;
}

@media screen and (max-width: 500px) {
  .el-input {
    width: 80vw;
    padding-bottom: 1rem;
  }
}

@media screen and (min-width: 501px) {
  .el-input {
    width: 20vw;
    padding-bottom: 1rem;
  }
}
</style>
